package com.ice.day726.sync;

import lombok.extern.slf4j.Slf4j;

/**
 * @Author ice2020x
 * @Date 2022-08-12 10:11
 * @Description:
 */
@Slf4j
public class Sync1 {
    public static void main(String[] args) {


        Room r = new Room();
        Thread t1 = new Thread(()->{
           for(int i=0;i<5000;i++){
               r.increment();
           }
        });

        Thread t2 = new Thread(()->{
            for(int i=0;i<5000;i++){
                r.decrement();
            }
        });

        t1.start();
        t2.start();

        System.out.println(r.getCounter());
    }


}
class  Room {
    private int counter = 0;

    public int getCounter() {
        synchronized(this){
            return counter;
        }
    }

    public void increment(){
        synchronized (this){
            counter++;
        }
    }

    public void decrement( ){
        synchronized(this){
            counter--;
        }
    }
}